package com.project.controller;

/**
 *	 瑜伽馆的前端控制器
 */
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.project.bean.CoachBean;
import com.project.bean.CommentBean;
import com.project.bean.FolloweBean;
import com.project.bean.GymBean;
import com.project.bean.GymDTO;
import com.project.bean.PageBean;
import com.project.bean.PostBean;
import com.project.bean.UserBean;
import com.project.service.ICommentService;
import com.project.service.IContractService;
import com.project.service.IFollowService;
import com.project.service.IGymService;
import com.project.service.IPostService;
import com.project.service.IStudentService;
import com.project.service.IUserService;
import com.project.util.UploadImgUtil;

@Controller
public class GymController {
	@Autowired
	private IGymService gymservice;
	@Autowired
	private IUserService userservice;	
	@Autowired
	private ICommentService commentService;
	@Autowired
	private IPostService postService;
	@Autowired
	private IStudentService studentService;
	@Autowired
	private IFollowService followService;
	@Autowired
	private IContractService contractService;
	
	//其他用户访问场馆
	@RequestMapping("/gym_idex_other")
	public String coachIndexOther(Model model,int cUId) {
		PageBean posts=postService.findUserPostsByPage(cUId, 1, 10);
		PageBean contracts = contractService.findContractByStateAndPage(4, cUId, 4, 1);
		UserBean gym1=userservice.findUserById(cUId);
		GymBean  gym2=gymservice.findGymById(cUId);
		model.addAttribute("posts", posts);
		model.addAttribute("contracts", contracts);
		model.addAttribute("gymBean", gym1);
		model.addAttribute("gymBean2", gym2);
		return "/gym_idex_other.html";
	}
	
	//添加关注
		@RequestMapping("/followGym")
		@ResponseBody
		public int addFollow(HttpServletRequest req,int cUId) {
			Integer uid=(Integer) req.getSession().getAttribute("user");				
			//判断是否已经关注
			if (followService.findFollow(uid, cUId)>0) {
				return 1;
			}else {
				if (followService.addFollow(uid, cUId)>0) {
					return 2;
				}
			}		
			return 0; 

		}
	

	//登录成功,进入主页
	@GetMapping("/gym/{uid}")
	public String showGym(@PathVariable("uid")Integer uId,ModelMap model,HttpServletRequest req) {	 
	 GymBean gym=gymservice.findGymById(uId);
	//将当前用户的id存入session
	 HttpSession session = req.getSession();	
	 session.setAttribute("user", uId);
	 model.addAttribute("gym", gym);
		return "redirect:/gymPosts?page=1&size=5";	
	}
		
	
	//进入完善(修改)信息页面
	@GetMapping("/gymInfo")
	public String gymMessage(HttpServletRequest req,ModelMap model){
		//获取当前用户的id
		Integer uid=(Integer) req.getSession().getAttribute("user");		
		UserBean userBean=userservice.findUserById(uid);
		GymBean gymBean=gymservice.findGymById(uid);
		System.out.println(userBean);
		System.out.println(gymBean);
		GymDTO gym=new GymDTO();
		gym.setuName(userBean.getuName());
		gym.setuAddress(userBean.getuAddress());
		gym.setgImg(gymBean.getgImg());
		gym.setgMessage(gymBean.getgMessage());
		
		model.addAttribute("gym", gym);
		
		return "/gym_info_change.html"; 
	
	}
	//进入查看附近学员
	@GetMapping("/gymFindStudents")
	public String showAllStudents(HttpServletRequest req,ModelMap map,int page,int size) {	 
		PageBean stuPage=studentService.findAllStudentToMap(page, size);
		map.addAttribute("stuPage", stuPage);
		System.out.println(stuPage);
		return "/gymmapteststudent.html";	
	}
	
	//返回主页
	@GetMapping("/gym")
	public String showOneStudent(HttpServletRequest req) {	 
		//获取当前用户的id
		Integer uid=(Integer) req.getSession().getAttribute("user");
		//System.out.println(uid);
		return "redirect:/gymPosts?page=1&size=5";	
	}
	
		
	
	//提交场馆信息
	@GetMapping("/gymInfo/version")
	public String updateGymInfo(GymDTO dto,HttpServletRequest req) {
		System.out.println(dto);
		Integer uid=(Integer) req.getSession().getAttribute("user");
		UserBean userBean = new UserBean();
		userBean.setuName(dto.getuName());
		userBean.setuAddress(dto.getuAddress());
		userBean.setuId(uid);
		int a=userservice.updateGym(userBean);
		System.out.println(a);
		GymBean gymBean=new GymBean();
		gymBean.setgUId(uid);
		gymBean.setgImg(dto.getgImg());
		gymBean.setgMessage(dto.getgMessage());
		int i = gymservice.updateGymInfo(gymBean);
		System.out.println(i);
		if(a>0&&i>0){
			return "redirect:/gymPosts?page=1&size=5";
		}
		return "/gym_info.html";
	}
	
	//显示我的动态
	@GetMapping("/gymPosts")
	public String showPosts(HttpServletRequest req,ModelMap map,int page,int size) {
		Integer uid=(Integer) req.getSession().getAttribute("user");
		PageBean pageBean=postService.findUserPostsByPage(uid, page, size);
		//找到推荐教练
		List<UserBean> list=userservice.findUsersByType(2);	
		map.addAttribute("Coachs", list);
		map.addAttribute("pageBean", pageBean);
		return "/gym_idex_self.html";
		
	}	
	
	//显示好友的动态
		@GetMapping("/gymFriendsPosts")
		@ResponseBody
		public PageBean showfriendsPosts(HttpServletRequest req,ModelMap map,int page,int size) {
			Integer uid=(Integer) req.getSession().getAttribute("user");
			PageBean pageBean=postService.findAllFollowPosts(uid, page, size);
			
			return pageBean;			
		}
		//查看粉丝
		@GetMapping("/gymFans")
		@ResponseBody
		public List<FolloweBean> findFansById(HttpServletRequest req){
			Integer uid=(Integer) req.getSession().getAttribute("user");
			
			List<FolloweBean> list=followService.findFansById(uid);
			return list;		
		}
		//查看关注
		@GetMapping("/gymFollow")
		@ResponseBody
		public List<FolloweBean> findFollowsById(HttpServletRequest req){
			Integer uid=(Integer) req.getSession().getAttribute("user");
			
			List<FolloweBean> list=followService.findFollowsById(uid);
			return list;	
		}

	//发布动态
	@PostMapping("/gymPost")
	public String addPost(String Message,HttpServletRequest req,MultipartFile imgName,ModelMap map) {	
		Integer uid=(Integer) req.getSession().getAttribute("user");
		PostBean post=new PostBean();
		String pictureName=UploadImgUtil.uploadimg(req, imgName); 
		post.setpImg(pictureName);
		post.setpUId(uid);
		post.setpMessage(Message);
        Date date=new Date();
		post.setpTime(date);
		System.out.println(post);
		postService.addPost(post);		
	    return "redirect:/gymPosts?page=1&size=4";
	
	}
	
	//发送评论
	@PostMapping("/gymPostComment")
	@ResponseBody
	public CommentBean addComment(String Message,HttpServletRequest req,MultipartFile imgName,ModelMap map) {	
		CommentBean comment = new CommentBean();
		comment.setCmMessage(Message);
		comment.setCmPId(3);
		comment.setCmUId(3);		
        Date date=new Date();
		comment.setCmTime(date);
		System.out.println(comment);
		commentService.addComment(comment);		
	    return comment;
	
	}
	//显示当前动态的评论
	@GetMapping("/gymPostComments")
	public String showComments(ModelMap map) {
		List<CommentBean> list=commentService.findCommentsByPost(43);		
		map.addAttribute("list", list);
		for (CommentBean commentBean : list) {
			System.out.println(commentBean);
		}
		return "/gym_idex_self.html";		
	}		

	
	
	
	/**
	 * 分页查询场馆
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/gym/{page}/{size}",method=RequestMethod.GET)
	@ResponseBody
	public PageBean showGymByPage(@PathVariable("page")int page,@PathVariable("size")int size) {
		PageBean bean = gymservice.findGymByPage(page, size);
		return bean;
	}

	


	
}
